﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;

namespace mesoBoard.Data.Repositories
{
    public class PollRepository : IPollRepository
    {
        private mbEntities db;

        public PollRepository(mbEntities mbDataContext)
        {
            this.db = mbDataContext;
        }

        public Poll GetPoll(int PollID)
        {
            return db.Polls.FirstOrDefault(x=>x.PollID == PollID);
        }

        public Poll GetPollByThread(int ThreadID)
        {
            return db.Polls.FirstOrDefault(x => x.PollID == ThreadID);
        }

        public IEnumerable<PollOption> GetPollOptions(int PollID)
        {
            return db.PollOptions.Where(x => x.PollID == PollID);
        }

        public void DeletePoll(int PollID)
        {
            db.Polls.DeleteObject(db.Polls.FirstOrDefault(x=>x.PollID == PollID));
            db.SaveChanges();
        }

        public void DeletePoll(Poll poll)
        {
            db.Polls.DeleteObject(poll);
            db.SaveChanges();
        }

        public string GetPollOptionsAsText(int PollID)
        {
            string text = "";
            foreach (PollOption p in GetPollOptions(PollID))
            {
                text += p.Text + Environment.NewLine;
            }
            return text;
        }

        public bool HasVotes(int PollID)
        {
            return GetPoll(PollID).PollOptions.Any(x => x.PollVotes.Count > 0);
        }

        public void CreatePoll(Poll ThePoll, IEnumerable<PollOption> Options, int ThreadID)
        {
            db.Threads.First(x => x.ThreadID == ThreadID).HasPoll = true;
            ThePoll.PollID = ThreadID;
            db.Polls.AddObject(ThePoll);
            db.SaveChanges();
            foreach (PollOption p in Options)
            {
                p.PollID = ThePoll.PollID;
                db.PollOptions.AddObject(p);
            }
            db.SaveChanges();
        }

        public void CreatePoll(Poll ThePoll)
        {
            db.Polls.AddObject(ThePoll);
            db.SaveChanges();
        }

        public void CreatePollOption(PollOption ThePollOption)
        {
            db.PollOptions.AddObject(ThePollOption);
            db.SaveChanges();

        }

        public void CastVote(int PollOptionID, int UserID)
        {
            db.PollVotes.AddObject(new PollVote
            {
                PollOptionID = PollOptionID,
                UserID = UserID,
            });
            db.SaveChanges();
        }

        public IEnumerable<PollVote> GetPollVotes(int pollID)
        {
            return db.PollVotes.Where(x => x.PollOption.PollID == pollID);
        }

        public bool HasVoted(int PollID, int UserID)
        {
            return db.PollVotes.Any(x => x.PollOption.PollID == PollID && x.UserID == UserID);
        }     
    }
}
